/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;

import Supporter.ImageSupporter;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Van persie
 */
public class ConveyorBelt extends MaterialObject {
    int wid = 20;        
    int hei = 15;
    int scalar;
    int Speech = 2;
    int beginChain = 0;
    int NumOfVisibleItems ;
    Vector listItem;
    
    private PetItem currDrag = null;
    ContextDialog ItemInfo = null;
    private boolean run = false;
    
    public ConveyorBelt(String backgPath,String name, int  x,int y, int wid,int hei )
    {
       
        super.GetGraphicsInfo(x, y, wid, hei);
        super.LoadImage(backgPath,false, wid, hei);
        SetAlphaBack();
                
        listItem = new Vector();        
        ItemInfo = new ContextDialog("/Plane.png");
        
        isVisible = true;
    }
     public void Check()
    {
        
    }
    public boolean Press(int x,int y)
    {
        for(int i = 0 ; i < listItem.size(); i++)       
            if(((PetItem)listItem.elementAt(i)).Press(x,y))
            {
                PetItem temp = (PetItem)listItem.elementAt(i);
                String []text = {"Hello World", "Abc"};
                
                int tempX,tempY;
                int wid = 100;
                int hei = 20 + 15*text.length;
                tempX = temp.getxPos() - wid;
                tempY = temp.getyPos() - hei ;
                if( tempX < 0)
                    tempX += (-tempX);
                
                ItemInfo.Show(text, tempX, tempY, wid, hei,-1,0 );
                setCurrDrag((PetItem)listItem.elementAt(i));
                return true;
            }
        return false;
    }
    public void Drag(int x, int y)
    {
        for(int i = 0 ; i < listItem.size(); i++)       
            ((PetItem)listItem.elementAt(i)).Drag(x, y);
        ItemInfo.Hide();
    }
    public boolean Release(int x, int y)
    {
        for(int i = 0 ; i < listItem.size(); i++)       
            ((PetItem)listItem.elementAt(i)).Release(x,y);
        currDrag = null;
        ItemInfo.Hide();
        return false;
        
    }
    public void Add(MaterialObject obj)
    {
        listItem.addElement(obj);
    }
    public void ArrangeItem()
    {
        
        scalar = ((MaterialObject)listItem.elementAt(0)).width;
        NumOfVisibleItems = (width - wid) / (scalar + wid);
        beginChain = 0;
        for(int i = 0; i  < listItem.size(); i++)     
        {
            ((MaterialObject)listItem.elementAt(i)).setxPos(xPos + wid*(i+1) + scalar*i);
            ((MaterialObject)listItem.elementAt(i)).setyPos(yPos + hei);
        }
             
                
    }
   
    public void setIsVisible(boolean  bit)
    {
        super.setIsVisible(bit);
        for(int i = 0 ; i < listItem.size(); i++)       
            ((MaterialObject)listItem.elementAt(i)).setIsVisible(bit);
    }
    public void Update()
    {
        //if(NumOfVisibleItems >= listItem.size())
        //    return;
        if(run == false)
            return;
        int x;
        int count = listItem.size();
        for(int i = 0 ; i < count; i++)       
        {
            x = ((MaterialObject)listItem.elementAt(i)).getxPos();
            ((MaterialObject)listItem.elementAt(i)).setxPos((x + Speech)%((count*wid + (count+1)*scalar)));
        }
        ItemInfo.MyUpdate();
        
    }

    public void paint(Graphics g)
    {
        if(isVisible == true)
        {
            
            g.drawImage(background, xPos, yPos,Graphics.TOP | Graphics.LEFT);
            ItemInfo.paint(g);
            for(int i=0; i <listItem.size(); i++)
                ((PetItem)listItem.elementAt(i)).paint(g);
        }        
    }

    /**
     * @return the run
     */
    public boolean isRun() {
        return run;
    }

    /**
     * @param run the run to set
     */
    public void setRun(boolean run) {
        this.run = run;
    }

    /**
     * @return the currDrag
     */
    public PetItem getCurrDrag() {
        return currDrag;
    }

    /**
     * @param currDrag the currDrag to set
     */
    public void setCurrDrag(PetItem currDrag) {
        this.currDrag = currDrag;
    }
    
}
